﻿using LoveEat.API.Write.Applications.Commands.Aquaculture;
using LoveEat.API.Write.Applications.Commands.Deep;
using LoveEat.API.Write.Applications.Commands.Goods;
using LoveEat.API.Write.Applications.Commands.Paths;
using LoveEat.API.Write.Applications.Commands.slaughter;
using LoveEat.ErrorCode;
using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace LoveEat.API.Write.Controllers
{
    /// <summary>
    /// 爱吃兔溯源API控制器—写
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class ManagementController : ControllerBase
    {
        /// <summary>
        /// 日志
        /// </summary>
        private readonly ILogger<ManagementController> logger;
        /// <summary>
        /// 中介者
        /// </summary>
        private readonly IMediator mediator;
        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="logger">日志</param>
        /// <param name="mediator">中介者</param>
        public ManagementController(ILogger<ManagementController> logger,IMediator mediator)
        {
            this.logger = logger;
            this.mediator = mediator;
        }
        /// <summary>
        /// 添加商品信息方法
        /// </summary>
        /// <param name="command">命令</param>
        /// <returns>返回任务</returns>
        [HttpPost]
        public async Task<APIResult<int>> AddGoods(AddGoodsCreatedCommand command)
        {
            try
            {
                logger.LogInformation("添加商品成功");
                return await mediator.Send(command);
            }
            catch (Exception ex)
            {
                logger.LogError("添加商品失败" + ex.Message);
                throw;
            }
        }

        /// <summary>
        /// 商品修改方法
        /// </summary>
        /// <param name="command">命令</param>
        /// <returns>返回任务</returns>
        [HttpPost]
        public async Task<APIResult<int>> UpdateGoods(UpdateGoodsCommand command)
        {
            try
            {
                logger.LogInformation("商品修改成功");
                return await mediator.Send(command);
            }
            catch (Exception ex)
            {
                logger.LogError("商品修改失败"+ex.Message);
                throw;
            }
        }

        /// <summary>
        /// 添加图片方法
        /// </summary>
        /// <param name="command">命令</param>
        /// <returns>返回任务</returns>
        [HttpPost]
        public async Task<APIResult<int>> AddPath(PathAddCommand command)
        {
            try
            {
                logger.LogInformation("图片添加数据库成功");
                return await mediator.Send(command);
            }
            catch (Exception ex)
            {
                logger.LogError("图片添加数据库失败" + ex.Message);
                throw;
            }
        }

        /// <summary>
        /// 逻辑删除方法
        /// </summary>
        /// <param name="command">命令</param>
        /// <returns>返回任务</returns>
        [HttpDelete]
        public async Task<APIResult<int>> DeleteGoods(DeleteGoodsCommand command)
        {
            try
            {
                logger.LogInformation("逻辑删除成功");
                return await mediator.Send(command);
            }
            catch (Exception ex)
            {
                logger.LogError("逻辑删除失败" + ex.Message);
                throw;  
            }
        }
        /// <summary>
        /// 批量逻辑删除
        /// </summary>
        /// <param name="command">命令</param>
        /// <returns>返回任务</returns>
        [HttpPost]
        public async Task<APIResult<int>> BatchDeleteGoods(BatchDeleteCommand command)
        {
            try
            {
                logger.LogInformation("批量逻辑删除成功");
                return await mediator.Send(command);
            }
            catch (Exception ex)
            {
                logger.LogError("批量逻辑删除失败" + ex.Message);
                throw;

            }
        }
        /// <summary>
        /// 养殖信息添加方法
        /// </summary>
        /// <param name="command">命令</param>
        /// <returns>返回任务</returns>
        [HttpPost]
        public async Task<APIResult<int>> AddAquaculture(AddAquacultureCommand command)
        {
            try
            {
                logger.LogInformation("养殖信息添加成功");
                return await mediator.Send(command);
            }
            catch (Exception ex)
            {
                logger.LogError("养殖信息添加失败"+ex.Message);
                throw;
            }
        }

        /// <summary>
        /// 保健信息删除方法
        /// </summary>
        /// <param name="command">命令</param>
        /// <returns>返回任务</returns>
        [HttpDelete]
        public async Task<APIResult<int>> DeleteHealt(DeleteHealthCommand command)
        {
            try
            {
                logger.LogInformation("保健信息删除成功");
                return await mediator.Send(command);
            }
            catch (Exception ex)
            {
                logger.LogError("保健信息删除失败" + ex.Message);
                throw;
            }
        }

        /// <summary>
        /// 深加工添加方法
        /// </summary>
        /// <param name="command">命令</param>
        /// <returns>返回任务</returns>
        [HttpPost]
        public async Task<APIResult<int>> AddDeep(AddDeepCommand command)
        {
            try
            {
                logger.LogInformation("深加工添加成功");
                return await mediator.Send(command);
            }
            catch (Exception ex)
            {
                logger.LogError("深加工添加失败" + ex.Message);
                throw;
            }
        }
        /// <summary>
        /// 屠宰信息添加方法
        /// </summary>
        /// <param name="command">命令</param>
        /// <returns>返回任务</returns>
        [HttpPost]
        public async Task<APIResult<int>> AddSlaughter(AddslaughterCommand command)
        {
            try
            {
                logger.LogInformation("屠宰信息添加成功");
                return await mediator.Send(command);
            }
            catch (Exception ex)
            {
                logger.LogError("屠宰信息添加失败" + ex.Message);
                throw;
            }
        }

        /// <summary>
        /// 屠宰信息（添加公司信息）
        /// </summary>
        /// <param name="command">命令</param>
        /// <returns>返回任务</returns>
        [HttpPost]
        public async Task<APIResult<int>> AddComp(AddCompCommand command)
        {
            try
            {
                logger.LogInformation("添加公司信息成功");
                return await mediator.Send(command);
            }
            catch (Exception ex)
            {
                logger.LogError("添加公司信息失败"+ex.Message);
                throw;
            }
        }


    }
}
